package net.blogways.ordermgr.util;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenlong
 * Created on 2018/3/2
 */
public class MongoManager {

  private  String host = null;

  private  int port = 27017;

  private  String host2 = null;

  private  int port2 = 27017;

  private  String host3 = null;

  private  int port3 = 27017;

  private  int poolSize = 10;

  private  int blockSize = 10;

  private String dataBase = "test";

  private String password = "";

  private String userName = "test";

  private MongoClient client = null;

  public int getChunkSizeBytes() {
    return chunkSizeBytes;
  }

  public void setChunkSizeBytes(int chunkSizeBytes) {
    this.chunkSizeBytes = chunkSizeBytes;
  }

  private int chunkSizeBytes = 1024 * 1024 * 1024;

  public MongoManager() {
  }

  @PostConstruct
  public void initialize() {
    MongoClientOptions options = MongoClientOptions.builder()
            .connectionsPerHost(poolSize)
            .threadsAllowedToBlockForConnectionMultiplier(blockSize)
            .build();

    if (password.equals("")){
      this.client = new MongoClient(
              new ServerAddress(this.host, this.port),
              options
      );
    } else {
      MongoCredential credential1 = MongoCredential.createCredential(userName, dataBase, password.toCharArray());

      List credential = new ArrayList();

      credential.add(credential1);
      this.client = new MongoClient(
              Arrays.asList(new ServerAddress(this.host, this.port),
                      new ServerAddress(this.host2, this.port2),
                      new ServerAddress(this.host3, this.port3)), credential,
              options
      );
    }
  }

  public MongoDatabase getDatabase(String database) {
    return this.client.getDatabase(database);
  }

  public String getHost1() {
    return host;
  }

  public int getPort1() {
    return port;
  }

  public String getHost2() {
    return host2;
  }

  public int getPort2() {
    return port2;
  }

  public String getHost3() {
    return host3;
  }

  public int getPort3() {
    return port3;
  }

  public void setHost(String host) {
    this.host = host;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public void setHost2(String host2) {
    this.host2 = host2;
  }

  public void setPort2(int port2) {
    this.port2 = port2;
  }

  public void setHost3(String host3) {
    this.host3 = host3;
  }

  public void setPort3(int port3) {
    this.port3 = port3;
  }

  public int getPoolSize() {
    return poolSize;
  }

  public void setPoolSize(int poolSize) {
    this.poolSize = poolSize;
  }

  public String getDataBase() {
    return dataBase;
  }

  public String getPassword() {
    return password;
  }

  public String getUserName() {
    return userName;
  }

  public void setDataBase(String dataBase) {
    this.dataBase = dataBase;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public int getBlockSize() {
    return blockSize;
  }

  public void setBlockSize(int blockSize) {
    this.blockSize = blockSize;
  }

}
